package com.example.demo.data;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.date.TimeInterval;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.ds.DSFactory;

import java.sql.SQLException;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <pre>
 *  描述:
 * </pre>
 *
 * @author Lucas Liu
 * @version 1.0.0
 * @date 2022/2/19 16:25
 */
public class TestUtil {
    public static void main(String[] args) {
        int weekend = 1645966522;

        int beginTimeOf20220225 = 1645721722;
        int endTimeOf20220225 = 1645804522;

        int timeOfLastMonth = 1642899322;
        int timeOf20211231 = 1640966122;

        int value = 5;

        int seed = 2;

        TimeInterval timer = DateUtil.timer();
        // 功率
        List<Long> metricIds = new ArrayList<Long>(){{
                    add(4697933366542085916L);
                    //add(8525998313540695581L);
                    //add(8625998313540695581L);
         }};
        // 电流
        //List<Long> metricIds = new ArrayList<Long>(){{
        //    add(-2665417781489259762L);
        //    //add(8925998313540695581L);
        //    //add(9035458925212461821L);
        //}};
        try {
            final AtomicInteger counter = new AtomicInteger(0);
            List<CompletableFuture<Void>> futures = Stream.of("-1050058464661897273",
                            "-1370700782722513142",
                            "-2205887562345444465",
                            "-4288407180237430205",
                            "-4989359218829087260",
                            "-5471500489670817306",
                    "1534998354617510153",
                    "4991207625032390815",
                    "5370977423465475031",
                    "5672460989510603625",
                    "604086299675127445").limit(7).map(
                    instanceid -> CompletableFuture.runAsync(() -> {
                        int time = timeOf20211231;
                        List<Entity> records = new ArrayList<>();
                        System.out.println(instanceid + "开始生成数据");
                        while (time <= timeOfLastMonth) {
                            records.add(
                                    Entity.create("MO_B_PM_VALUE_0")
                                            .set("instanceid", instanceid)
                                            // 只给一路指标插入记录
                                            .set("metric", metricIds.get(0))
                                            .set("ts", time)
                                            .set("value", new Random().nextInt(value))
                                            .set("tags", "0")
                            );

                            // 一天1个点
                            time += 60 * 60 * 24;
                        }
                        while (time <= beginTimeOf20220225) {
                            records.add(
                                    Entity.create("MO_B_PM_VALUE_0")
                                            .set("instanceid", instanceid)
                                            // 只给一路指标插入记录
                                            .set("metric", metricIds.get(0))
                                            .set("ts", time)
                                            .set("value", new Random().nextInt(value))
                                            .set("tags", "0")
                            );

                            // 一小时1个点
                            time += 60 * 60;
                        }
                        // 2月25号9点前的数据，一个小时一个点
                        try {
                            Db.use(DSFactory.get("mo")).insert(records);
                            System.out.println(instanceid + "   25号前数据点插入：" + records.size());
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                        records.clear();
                        while (time <= endTimeOf20220225) {
                            // 每路指标都插入数据
                            //for (Long metricId : metricIds) {
                                // 6分钟内随机分布
                                //int recordTime = time + new Random().nextInt(6 * 60);
                                int recordTime = time;
                                records.add(
                                        Entity.create("MO_B_PM_VALUE_0")
                                                .set("instanceid", instanceid)
                                                .set("metric", metricIds.get(0))
                                                .set("ts", recordTime)
                                                .set("value", value/2 + new Random().nextInt(seed))
                                                .set("tags", "0")
                                );
                            //}
                            // 6分钟一个点， 一小时10个点
                            time += 6 * 60;
                        }
                        try {
                            Db.use(DSFactory.get("mo")).insert(records);
                            System.out.println(instanceid + "   6分钟间隔点插入：" + records.size());
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                        records.clear();
                        while (time <= weekend) {
                            records.add(
                                    Entity.create("MO_B_PM_VALUE_0")
                                            .set("instanceid", instanceid)
                                            // 只给一路指标插入记录
                                            .set("metric", metricIds.get(0))
                                            .set("ts", time)
                                            .set("value", new Random().nextInt(value))
                                            .set("tags", "0")
                            );
                            // 一小时1个点
                            time += 60 * 60;
                        }
                        try {
                            Db.use(DSFactory.get("mo")).insert(records);
                            System.out.println(instanceid + "    周末数据点插入：" + records.size());
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                        System.out.println("本轮：" + counter.incrementAndGet());
                    })
            ).collect(Collectors.toList());

            CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).thenApply( e->futures.stream().map(CompletableFuture::join).collect(Collectors.toList())).get();
        } catch (Exception e)  {
            System.out.println(e.getMessage());
        }
        System.out.println("耗时：" + timer.intervalPretty());
    }
}
